//用户管理

layui.config({
  base: '/'
}).extend({
  flying: 'flying'
}).define(['index', 'flying'], function (exports) {
  let admin = layui.admin;
  let flying = layui.flying;
  let form = layui.form;
  let table = layui.table;

  flying.table({
    elem: '#LAY-user-table',
    url: '/user/getUserPageList', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'userId', title: '用户ID', minWidth: 90}
      , {field: 'username', title: '用户名', minWidth: 90}
      , {field: 'name', title: '姓名', minWidth: 100}
      , {field: 'orgName', title: '所属组织', minWidth: 110}
      , {field: 'mobile', title: '手机', minWidth: 120}
      , {field: 'email', title: '邮箱', minWidth: 160}
      , {field: 'gender', title: '性别', minWidth: 60, templet: function (d) {
          if (d.gender == 'M') {
            return '<span class="layui-bg-blue layui-badge-rim">男</span>';
          } else if (d.gender == 'F') {
            return '<span class="layui-bg-green layui-badge-rim">女</span>';
          } else {
            return '';
          }
        }, unresize: true
      }
      , {field: 'status', title: '使用状态', minWidth: 90, templet: '#statusSwitchTpl', unresize: true}
      , {field: 'locked', title: '锁定状态', minWidth: 90, templet: function (d) {
          if (d.locked == 'Y') {
            return '<span class="layui-bg-orange layui-badge-rim">锁定</span>';
          } else if (d.locked == 'N') {
            return '<span class="layui-bg-green layui-badge-rim">正常</span>';
          } else {
            return '';
          }
        }, unresize: true
      }
      // ,{field: 'picture', title: '头像', width: 100, templet: '#imgTpl'}
      , {field: 'lastLoginTime', title: '最后登录时间', templet: function (d) {
          if (d.lastLoginTime) {
            return layui.util.toDateString(d.lastLoginTime, "yyyy-MM-dd HH:mm:ss");
          } else {
            return '';
          }
        }, minWidth: 170, sort: true
      }
      , {field: 'updatedBy', title: '修改人', minWidth: 90}
      , {field: 'updatedTime', title: '修改时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>', minWidth: 170, sort: true}
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#user-table'}
    ]
  });

  //监听工具条
  table.on('tool(LAY-user-table)', function (obj) {
    let line_data = obj.data;
    if (obj.event === 'del') {
      if ('10000' == line_data.userId) {
        layer.msg('Admin用户不允许删除！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
        return;
      }

      layer.confirm('真的删除行么？', function (index) {
        //提交 Ajax 成功后，更新表格中的数据
        admin.req({
          url: '/user/del/' + line_data.userId
          , type: 'post'
          , done: function (res) {
            //执行成功
            layer.msg(res.msg, {icon: 1});
            table.reload('LAY-user-table'); //数据刷新
            layer.close(index); //关闭弹层
          }
        });
      });
    } else if (obj.event === 'edit') {
      // let tr = $(obj.tr);
      layer.open({
        type: 2
        , title: '修改'
        , content: '/user/turn/edit?id=' + line_data.id
        , area: ['98.3%', '98.3%']
        , resize: false
        , yes: function (index, layero) {
        }
        , cancel: function (index, layero) {
          layer.close(index);
          table.reload('LAY-user-table'); //数据刷新
        }
        , end: function () {
          table.reload('LAY-user-table'); //数据刷新
        }
      });
    }
  });

  //对外暴露的接口
  exports('useradmin', {});
});